package com.hs.mod_archive.helper

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.hs.framework.utils.getStringFromResource
import com.hs.mod_archive.R
import com.hs.mod_archive.databinding.ItemReaderBinding
import com.hs.mod_archive.view.MyListView
import com.hs.room.model.archive.DevReader


class DevReaderHelper(
    footPageListView: MyListView, private val itemClickListener: OnItemClickListener?,
    footSearchCallback: FootSearchCallback?
) :
    ListViewHelper(footPageListView, footSearchCallback) {
    private val mContext: Context = footPageListView.context
    var data: DevReader? = null
    private val viewHolders = arrayOfNulls<ItemReaderBinding>(
        perPageCount
    )
    private val mParentView: LinearLayout = footPageListView.mViewList


    private var datas: List<DevReader>? = null


    fun setListDataAndRefresh(entityList: List<DevReader>?) {
        datas = entityList
        if (null == viewHolders[0]) {
            for (i in 0 until perPageCount) {
                val binding =
                    ItemReaderBinding.inflate(
                        LayoutInflater.from(mContext),
                        mParentView,
                        true
                    )
                viewHolders[i] = binding
                if (isEvenOrOdd(i)) {
                    binding.root.setBackgroundColor(mColorBgOddNumberLine)
                }
                binding.tvOpt.setOnClickListener { itemClickListener!!.onItemClickDelete(i) }
            }
        }
        if (datas!!.isNotEmpty()) {
            mParentView.visibility = View.VISIBLE
            val newListSize = datas!!.size
            for (i in 0 until perPageCount) {
                if (i >= newListSize) {
                    val recordBinding = viewHolders[i]
                    recordBinding!!.root.visibility = View.INVISIBLE
                } else {
                    val holder = viewHolders[i]
                    holder!!.root.visibility = View.VISIBLE
                    data = datas!![i]
                    holder.tvName.text = data!!.name
                    holder.tvNo.text = "${data!!.localId}"
                    holder.tvMac.text = "${data!!.boardMac}"
                    holder.tvStatus.setText(
                        if (0 == data!!.status) getStringFromResource(R.string.enable) else getStringFromResource(
                            R.string.disable
                        )
                    )
                }
            }
        } else {
            mParentView.visibility = View.INVISIBLE
            for (binding in viewHolders) {
                binding!!.root.visibility = View.INVISIBLE
            }
        }
    }
}
